package com.lottery.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lottery.common.contants.Constants;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.dao.MemberInfoMapper;
import com.lottery.common.entity.MemberInfo;
import com.lottery.common.entity.PaymentRecords;
import com.lottery.pay.Constants.PaymentConstant;
import com.lottery.pay.service.BalanceService;
import com.lottery.pay.service.PaymentRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

import static com.lottery.common.contants.Constants.TYPE_ZERO;

@Service
@Slf4j
public class BalanceServiceImpl implements BalanceService {
    private ReQueryResult result = new ReQueryResult();


    @Resource
    private MemberInfoMapper memberInfoMapper;
    @Resource
    private PaymentRecordsService prs;

    @Override
    public ReQueryResult createPayment(PaymentRecords records) {
        log.info("余额支付创建订单");
        result = new ReQueryResult();
        MemberInfo memberInfo=getMemberBalance(records.getMemberId());
        if (memberInfo.getBalance().compareTo(records.getTotalFee())<0){
            result.error(PaymentConstant.USER_BALANCE_LACK);
            return result;
        }
        memberInfo.setBalance(memberInfo.getBalance().subtract(records.getTotalFee()));
        memberInfoMapper.updateById(memberInfo);
        records.setStatus(TYPE_ZERO);
        boolean flag=prs.updateById(records);
        result.success(PaymentConstant.PAY_SUCCESS);
        return result;
    }

    @Override
    public ReQueryResult queryPayment(PaymentRecords records) {
        return null;
    }

    @Override
    public ReQueryResult refundPayment(PaymentRecords records) {
        return null;
    }

    @Override
    public ReQueryResult queryRefundPayment(PaymentRecords records) {
        return null;
    }

    @Override
    public ReQueryResult closePayment(PaymentRecords records) {
        return null;
    }

    @Override
    public ReQueryResult resultDispose(HttpServletRequest request) {
        return null;
    }

    @Override
    public ReQueryResult createGoldePayment(PaymentRecords records) {
        MemberInfo memberInfo=getMemberBalance(records.getMemberId());
        if (memberInfo.getGold().compareTo(records.getTotalFee())<0){
            result.error(PaymentConstant.USER_BALANCE_LACK);
            return result;
        }
        memberInfo.setGold(memberInfo.getGold().subtract(records.getTotalFee()));
        memberInfoMapper.updateById(memberInfo);
        records.setStatus(TYPE_ZERO);
        result.success(PaymentConstant.PAY_SUCCESS);
        return result;
    }

    public MemberInfo getMemberBalance(Long id){
        QueryWrapper<MemberInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","balance","gold");
        queryWrapper.eq("id",id);
        MemberInfo memberInfo=memberInfoMapper.selectOne(queryWrapper);
        return memberInfo;
    }

}
